package com.zhangtai.modules.service.impl.common;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.MailRecordDao;

import com.zhangtai.modules.entity.common.MailRecordEntity;

import com.zhangtai.modules.service.common.MailRecordService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class MailRecordServiceImpl extends ServiceImpl<MailRecordDao, MailRecordEntity> implements MailRecordService {

    @Override
    public PageResult queryPage(@RequestParam(value = "收件人",required = false) String name,
                                @RequestParam(value = "发件人",required = false)String sendName,
                                @RequestParam(value = "收件邮箱",required = false)String receive,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "oid", required = false) Long oid,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<MailRecordEntity> page = this.page(
                new Page<MailRecordEntity>(pageNum,pageSize),
                new QueryWrapper<MailRecordEntity>().eq(status!=null,"status",status)
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .like(!StringUtils.isEmpty(sendName),"send_name",sendName)
                        .like(!StringUtils.isEmpty(receive),"receive",receive)
                        .eq("type",0)
                        .eq(oid!=null,"send_oid",oid)
                        .orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public MailRecordEntity getByIdE(Long id) {
            MailRecordEntity one = this.getOne(new QueryWrapper<MailRecordEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(MailRecordEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(MailRecordEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            MailRecordEntity entity = new MailRecordEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}